package com.itheima.service;

import com.alibaba.druid.sql.ast.statement.SQLIfStatement;
import com.itheima.common.R;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.pojo.DishDto;
import com.itheima.pojo.Employee;
import com.itheima.pojo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wuyiqiang
 * @2022/6/13 19:58
 */
@Service
public class EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employee
     * @return
     */
    public Employee login(Employee employee) {
        return employeeMapper.Login(employee);
    }

    /**
     * 分页查询
     *
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    public R<Page> page(String name, Integer page, Integer pageSize) {
        Integer pageMin = (page - 1) * pageSize;
        List<Employee> page1 = employeeMapper.page(name, pageMin, pageSize);

        Page page2 = new Page();

        //设置返回页的总条数
        page2.setTotal(employeeMapper.countPage());
        //设置返回页的每页数量
        page2.setSize(pageSize);
        if (page1 != null) {
            //设置返回页返回类对象的当前页
            page2.setCurrent(page);
            //将获得到的数据写进返回类中
            page2.setRecords(page1);
            return R.success(page2);
        } else {
            return R.error("没有数据了");
        }


    }

    /**
     * 新增员工
     *
     * @param employee
     * @return
     */
    public int add(Employee employee) {
        return employeeMapper.add(employee);
    }

    /**
     * 修改员工
     * @param employee
     * @return
     */
    public int update(Employee employee){
        return employeeMapper.update(employee);
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    public Employee updateById(String id){
       return employeeMapper.updateById(id);
    }
}
